package com.angel.utils;

import com.angel.shiro.UserAuthorizingRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.UnavailableSecurityManagerException;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.subject.Subject;

public class UserUtils {
    /**
     * 根据ID获取用户
     * @return 取不到返回null
     */
    public static  UserAuthorizingRealm.Principal getPrincipal(){
        try {
            Subject subject = SecurityUtils.getSubject();
            UserAuthorizingRealm.Principal principal = (UserAuthorizingRealm.Principal) subject.getPrincipal();
            if (principal != null) {
                return principal;
            }
        } catch (UnavailableSecurityManagerException e) {

        } catch (InvalidSessionException e) {

        }
        return null;
    }
}
